package com.ggqq.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * 该配置类是Swagger配置类，浏览器访问http://localhost:8081/swagger-ui.html
 * 端口号是和springboot的端口号一致
 * 在正式发布的时候，关闭Swagger！！！ 出于安全考虑而且节省内存
 * */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    //多个分组
//    @Bean
//    public Docket docket1(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
//    }
//    @Bean
//    public Docket docket2(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
//    }
//    @Bean
//    public Docket docket3(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("group3");
//    }

    //配置Swagger的Docket的Bean实例
    @Bean
    public Docket docket(Environment environment){
        //配置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev","pro");
        //获取项目的环境：
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("gq-blog")
                .enable(flag)//swagger开关，true为开
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ggqq.controller"))//RequestHandlerSelectors配置要扫描接口的方式//basePackage指定要扫描的包
                .build();
    }

    //配置Swagger信息
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("gq-blog","https://blog.csdn.net/Mcdull__?spm=1001.2014.3001.5343", "2370084686@qq.com");
        return new ApiInfo(
                "gq-blog-api",
                "路漫漫其修远兮，吾将上下而求索",
                "1.0",
                "https://blog.csdn.net/Mcdull__?spm=1001.2014.3001.5343",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
        );
    }

}
